package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.utils.LanguageUtiles;
import com.umeng.analytics.MobclickAgent;
import com.zyyoona7.wheel.WheelView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

public class X3HomeTyreTimeSelectDialog extends X3BaseBottomSheetDialog implements View.OnClickListener {

    private TextView tv_title;
    private TextView tv_cancel;
    private TextView tv_affirm;
    private LinearLayout ll_select;
    private ImageView iv_select;
    private WheelView<String> wv_year;
    private WheelView<String> wv_month;

    private int currentYear;
    private int currentMonth;
    private String sn;

    private boolean isSelect;
    private List<String> currentMonthList;

    public X3HomeTyreTimeSelectDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_title = findViewById(R.id.tv_title);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_affirm = findViewById(R.id.tv_affirm);
        wv_year = findViewById(R.id.wv_year);
        wv_month = findViewById(R.id.wv_month);
        ll_select = findViewById(R.id.ll_select);
        iv_select = findViewById(R.id.iv_select);

        tv_cancel.setOnClickListener(this);
        tv_affirm.setOnClickListener(this);
        ll_select.setOnClickListener(this);

        Calendar calendar = Calendar.getInstance();
        currentYear = calendar.get(Calendar.YEAR);
        currentMonth = calendar.get(Calendar.MONTH) + 1;

        int selectYear = 0;
        List<String> yearList = new ArrayList<>();
        for (int i = 0; i <= (currentYear - 1990); i++) {
            yearList.add((1990 + i) + "");
            if ((1990 + i) == 2010) {
                selectYear = i;
            }
        }

        List<String> monthList = Arrays.asList(mContext.getResources().getStringArray(R.array.select_month));

        currentMonthList = new ArrayList<>();
        for (int i = 0; i <= monthList.size(); i++) {
            if (i < currentMonth) {
                currentMonthList.add(monthList.get(i));
            }
        }

        wv_year.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
        wv_year.setSoundEffectResource(R.raw.x3_button_choose);
        wv_year.setSoundEffect(true);
        wv_year.setPlayVolume(0.5f);
        wv_year.setData(yearList);
        wv_year.setSelectedItemPosition(selectYear);
        wv_year.setOnItemSelectedListener((wheelView, data, position) -> {
            if (data.equals(currentYear + "")) {
                wv_month.setData(currentMonthList);
            }else {
                wv_month.setData(monthList);
            }
        });

        wv_month.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
        wv_month.setSoundEffectResource(R.raw.x3_button_choose);
        wv_month.setSoundEffect(true);
        wv_month.setPlayVolume(0.5f);
        wv_month.setData(monthList);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_home_tyre_time_number_picker;
    }

    /**
     * 设置选中的月份
     * @param year
     * @param month
     */
    public void setSelect(int year,int month) {
        List<String> data = wv_year.getData();
        int yearIndex = 0;
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).equals(year + "")) {
                yearIndex = i;
            }
        }
        wv_year.setSelectedItemPosition(yearIndex);

        List<String> selectedItemData = wv_month.getData();
        int monthIndex = 0;
        String monthStr = (month < 10) ? "0" + month : month + "";
        for (int i = 0; i < selectedItemData.size(); i++) {
            if (selectedItemData.get(i).equals(monthStr)) {
                monthIndex = i;
            }
        }
        wv_month.setSelectedItemPosition(monthIndex);

        //tv_title.setText(title + "");

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.tv_affirm) {
            dismiss();
            if (iOnClickTyreYear != null) {
                boolean zhRCN = LanguageUtiles.isZhRCN();
                String tyreYear;
                if (zhRCN) {
                    tyreYear = wv_year.getSelectedItemData() + "-" + wv_month.getSelectedItemData();
                } else {
                    tyreYear = wv_month.getSelectedItemData() + ", " + wv_year.getSelectedItemData();
                }
                String month;
                int i = wv_month.getSelectedItemPosition() + 1;
                month = (i < 10) ? "0" + i : i + "";
                iOnClickTyreYear.iOnClickTyreYearGet(sn, tyreYear, isSelect, wv_year.getSelectedItemData() + "-" + month);
            }
        } else if (id == R.id.ll_select) {
            if (isSelect) {
                iv_select.setImageResource(R.mipmap.x3_car_no_select);
            } else {
                iv_select.setImageResource(R.mipmap.x3_car_select);
            }
            isSelect = !isSelect;
        }
    }

    public void setShowSn(String sn) {
        this.sn = sn;
    }

    IOnClickTyreYear iOnClickTyreYear;

    public void setiOnClickTyreYear(IOnClickTyreYear iOnClickTyreYear) {
        this.iOnClickTyreYear = iOnClickTyreYear;
    }

    public interface IOnClickTyreYear {
        void iOnClickTyreYearGet(String sn, String yearMonth,boolean isAll,String selectYear);
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("初检信息选择轮胎年限弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("初检信息选择轮胎年限弹窗");
    }
}
